//
//  LoginViewModel.m
//  JiZhuiAn
//
//  Created by roger lin on 16/6/20.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "LoginViewModel.h"

@implementation LoginViewModel
@synthesize canLogin;

- (LoadingRACCommand *)loginCommand
{
    if (!_loginCommand) {
        @weakify(self);
        _loginCommand = [[LoadingRACCommand alloc]initWithEnabled:self.validSignal signalBlock:^RACSignal *(id input) {
            @strongify(self);
            return [[APIService shareService] loginWithPhoneNumber:self.phoneNumber Password:self.password];
        }];
    }
    return _loginCommand;
}

- (RACCommand *)getSpineTestDataCommand
{
    if (!_getSpineTestDataCommand) {
        _getSpineTestDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            return [[APIService shareService] getSpineTestData];
        }];
    }
    return _getSpineTestDataCommand;
}

- (RACSignal *)validSignal
{
    if (!_validSignal) {
        _validSignal = [RACSignal combineLatest:@[RACObserve(self, phoneNumber),
                                                  RACObserve(self, password)] reduce:^(NSString *phoneNumber, NSString *password){
                                                      if ([phoneNumber length] > 5 && [password length] > 0) {
                                                          canLogin = YES;
                                                          return @(1);
                                                      }
                                                      else{
                                                          canLogin = NO;
                                                          return @(0);
                                                      }
                                                  }];
                        }
    return _validSignal;
}

@end
